package com.tianji.learning.mq;

import com.tianji.learning.domain.po.PointsRecord;
import com.tianji.learning.service.IPointsRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import static com.tianji.common.constants.MqConstants.Exchange.POINT_RECORD_EXCHANGE;

/**
 * @Author: 19311
 * @CreateTime: 2025-03-28
 * @Description: 积分改变监听器
 * @Version: 1.0
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class PointChangeListener {

    private final IPointsRecordService pointsRecordService;

    // 签到获取积分的队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "points.sign.change.queue", durable = "true"),
            exchange = @Exchange(name = POINT_RECORD_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = "points.sign.change"
    ))
    public void pointChange(PointsRecord pointsRecord) {
        pointsRecordService.addPointsRecord(pointsRecord);
    }

    // 回答问题获取积分的队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "points.reply.change.queue", durable = "true"),
            exchange = @Exchange(name = POINT_RECORD_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = "points.reply.change"
    ))
    public void pointChangeReply(PointsRecord pointsRecord) {
        pointsRecordService.addPointsRecord(pointsRecord);
    }
}
